<?php

class FichesForum extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('ActionsDeCommunication/Forum');
		$this->load->model('ActionsDeCommunication/FicheForum');
		$this->load->model('ActionsDeCommunication/EvaluationEleve');
		$this->load->model('Utilisateurs/Enseignant');
		$this->load->model('Utilisateurs/Etudiants');
		
		include model_chemin('fonctions');
	}
	
	public function creer($idForum)
	{
		include model_chemin('putDateJJMMAAAA');
		
		$data['onglet_ouvert'] = 'actiondecommunication';
		
		$data['modif'] = false;
		$data['forum'] = Forum::findById($idForum);
		$data['message'] 			= $this->session->flashdata('message');
		$data['dureeEffective'] = $this->session->flashData('dureeEffective');
		$data['tempsTrajet'] 	= $this->session->flashData('tempsTrajet');
		$data['commentaire'] 	= $this->session->flashData('commentaire');
		$data['duree1'] 	= $this->session->flashData('duree1');
		$data['commentaire1'] 	= $this->session->flashData('commentaire1');
		$data['duree2'] 	= $this->session->flashData('duree2');
		$data['commentaire2'] 	= $this->session->flashData('commentaire2');
			
		$this->load->view('Global/entete.php', $data);
 		$this->load->view('FicheForum/v_formulaire.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function creation($idForum)
	{
		$erreur=false;
		$forum=Forum::findById($idForum);
		if(FicheForum::checkFBF($_POST['DureeEffective'], $_POST['TempsTrajet'], $_POST['Accueil'], $_POST['Emplacement'], $_POST['Circulation'], $_POST['Frequentation'], $_POST['Interet']))		
		{
			if(isset($_POST['Aucun']))
			{
				EvaluationEleve::addEvaluation(secur($forum->getEtudiant1()->getId(),'int'),secur($idForum,'int'), 0, null, null, null, null, null, null);
				EvaluationEleve::addEvaluation(secur($forum->getEtudiant2()->getId(),'int'),secur($idForum,'int'), 0, null, null, null, null, null, null);
			}
			else
			{
				if($_POST["Present1"] == 1)
				{
					if(EvaluationEleve::checkEvaluation($_POST['Presence1'], $_POST['Duree1'], $_POST['Tenue1'], $_POST['Oral1'], $_POST['Logistique1']))
						EvaluationEleve::addEvaluation(secur($forum->getEtudiant1()->getId(),'int'), secur($idForum,'int'), $_POST['Present1'], $_POST['Presence1'], $_POST['Duree1'], $_POST['Tenue1'], $_POST['Oral1'], $_POST['Logistique1'], secur($_POST['Commentaire1'], 'string'));
					else 
					{
						$this->session->set_flashData('duree1', $_POST['Duree1']);
						$this->session->set_flashData('commentaire1', $_POST['Commentaire1']);
						$erreur=true;
					}
				}
				else 
					EvaluationEleve::addEvaluation(secur($forum->getEtudiant1()->getId(),'int'), secur($idForum,'int'),0, null, null, null, null, null, null);
				
				if($_POST["Present2"] == 1)
				{
					if(EvaluationEleve::checkEvaluation($_POST['Presence2'], $_POST['Duree2'], $_POST['Tenue2'], $_POST['Oral2'], $_POST['Logistique2']))
						EvaluationEleve::addEvaluation(secur($forum->getEtudiant2()->getId(), 'int'), secur($idForum,'int'),$_POST['Present2'], $_POST['Presence2'], $_POST['Duree2'], $_POST['Tenue2'], $_POST['Oral2'], $_POST['Logistique2'], secur($_POST['Commentaire2'], 'string'));
					else
					{
						$this->session->set_flashData('duree2', $_POST['Duree2']);
						$this->session->set_flashData('commentaire2', $_POST['Commentaire2']);
						$erreur=true;
					}
				}	
				else
					EvaluationEleve::addEvaluation(secur($forum->getEtudiant2()->getId(),'int'), secur($idForum,'int'),0, null, null, null, null, null, null);
			}
		}
		else
		{
			$erreur=true;
		}
		if($erreur)
		{
			$this->session->set_flashData('dureeEffective', $_POST['DureeEffective']);
			$this->session->set_flashData('tempsTrajet', $_POST['TempsTrajet']);
			$this->session->set_flashData('commentaire', $_POST['Commentaire']);
			redirect(base_url().'index.php/FichesForum/creer/'.$idForum);
		}
		else
		{
			FicheForum::addFBF(secur($idForum, 'int'), $_POST['DureeEffective'], $_POST['TempsTrajet'], $_POST['Accueil'], $_POST['Emplacement'], $_POST['Circulation'], $_POST['Frequentation'], $_POST['Interet'], secur($_POST['Commentaire'], 'string'), 0);
			redirect(base_url().'index.php/FichesForum/confirmer/'.$idForum);
		}
	}
	
	public function modifier($idForum)
	{
		include model_chemin('putDateJJMMAAAA');
		include model_chemin('putHoraireHHhMM');
	
		$data['onglet_ouvert'] = 'actiondecommunication';
	
		$data['modif'] = true;
		$data['forum'] = Forum::findById($idForum);
		$data['fbf'] = FicheForum::findById($idForum);
		$data['ee1'] = EvaluationEleve::findById($idForum, $data['forum']->getEtudiant1()->getId());
		$data['ee2'] = EvaluationEleve::findById($idForum, $data['forum']->getEtudiant2()->getId());
		
		$data['message'] 			= $this->session->flashdata('message');
		$data['dureeEffective'] = $this->session->flashData('dureeEffective');
		$data['tempsTrajet'] 	= $this->session->flashData('tempsTrajet');
		$data['commentaire'] 	= $this->session->flashData('commentaire');
		$data['duree1'] 	= $this->session->flashData('duree1');
		$data['commentaire1'] 	= $this->session->flashData('commentaire1');
		$data['duree2'] 	= $this->session->flashData('duree2');
		$data['commentaire2'] 	= $this->session->flashData('commentaire2');
			
		$this->load->view('Global/entete.php', $data);
		$this->load->view('FicheForum/v_formulaire.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function modification($idForum)
	{
		$erreur=false;
		$forum=Forum::findById($idForum);
		$fbf = FicheForum::findById($idForum);
		$ee1 = EvaluationEleve::findById($idForum, $forum->getEtudiant1()->getId());
		$ee2 = EvaluationEleve::findById($idForum, $forum->getEtudiant2()->getId());
		if(FicheForum::checkFBF($_POST['DureeEffective'], $_POST['TempsTrajet'], $_POST['Accueil'], $_POST['Emplacement'], $_POST['Circulation'], $_POST['Frequentation'], $_POST['Interet']))
		{
			if(isset($_POST['Aucun']))
			{
				$ee1->modifyEvaluation(0, null, null, null, null, null, null);
				$ee2->modifyEvaluation(0, null, null, null, null, null, null);
			}
			else
			{
				if($_POST["Present1"] == 1)
				{
					if(EvaluationEleve::checkEvaluation($_POST['Presence1'], $_POST['Duree1'], $_POST['Tenue1'], $_POST['Oral1'], $_POST['Logistique1']))
						$ee1->modifyEvaluation($_POST['Present1'], $_POST['Presence1'], $_POST['Duree1'], $_POST['Tenue1'], $_POST['Oral1'], $_POST['Logistique1'], secur($_POST['Commentaire1'], 'string'));
					else
					{
						$this->session->set_flashData('duree1', $_POST['Duree1']);
						$this->session->set_flashData('commentaire1', $_POST['Commentaire1']);
						$erreur=true;
					}
				}
				else
					$ee1->modifyEvaluation(0, null, null, null, null, null, null);
	
				if($_POST["Present2"] == 1)
				{
					if(EvaluationEleve::checkEvaluation($_POST['Presence2'], $_POST['Duree2'], $_POST['Tenue2'], $_POST['Oral2'], $_POST['Logistique2']))
						$ee2->modifyEvaluation($_POST['Present2'], $_POST['Presence2'], $_POST['Duree2'], $_POST['Tenue2'], $_POST['Oral2'], $_POST['Logistique2'], secur($_POST['Commentaire2'], 'string'));
					else
					{
						$this->session->set_flashData('duree2', $_POST['Duree2']);
						$this->session->set_flashData('commentaire2', $_POST['Commentaire2']);
						$erreur=true;
					}
				}
				else
					$ee2->modifyEvaluation(0, null, null, null, null, null, null);
			}
		}
		else
		{
			$erreur=true;
		}
		if($erreur)
		{
			$this->session->set_flashData('dureeEffective', $_POST['DureeEffective']);
			$this->session->set_flashData('tempsTrajet', $_POST['TempsTrajet']);
			$this->session->set_flashData('commentaire', $_POST['Commentaire']);
			redirect(base_url().'index.php/FichesForum/modifier/'.$idForum);
		}
		else
		{
			$fbf->modifyFBF($_POST['DureeEffective'], $_POST['TempsTrajet'], $_POST['Accueil'], $_POST['Emplacement'], $_POST['Circulation'], $_POST['Frequentation'], $_POST['Interet'], secur($_POST['Commentaire'], 'string'), 0);
			redirect(base_url().'index.php/FichesForum/confirmer/'.$idForum);
		}
	}
	
	public function confirmer($idForum)
	{
		include model_chemin('putDateJJMMAAAA');
		include model_chemin('putHoraireHHhMM');
	
		$data['onglet_ouvert'] = 'actiondecommunication';
	
		$data['forum'] = Forum::findById($idForum);
		$data['fbf'] = FicheForum::findById($idForum);
		$data['ee1'] = EvaluationEleve::findById($idForum, $data['forum']->getEtudiant1()->getId());
		$data['ee2'] = EvaluationEleve::findById($idForum, $data['forum']->getEtudiant2()->getId());
			
		$this->load->view('Global/entete.php', $data);
		$this->load->view('FicheForum/v_confirmationFormulaire.php', $data);
		$this->load->view('Global/piedpage.php');
	}
	
	public function confirmation($idForum)
	{
		$data['onglet_ouvert'] = 'actiondecommunication';
	
		$fbf = FicheForum::findById($idForum);
		$forum = Forum::findById($idForum);
		$fbf->confirmFBF();
		if($forum->getTitre()==Forum::LUNDI || $forum->getTitre()==Forum::LUNDI2)
			redirect(base_url().'index.php/LundisInfo/voirLundisInfo/'.$forum->getAction()->getId());
		else
			redirect(base_url().'index.php/Forums/detailForum/'.$idForum);
	}
}